import Vue from 'vue'
import Router from 'vue-router'
import store from './store'
import StorageAPI from './api/storage'

import Login from './components/login/Login.vue';
import Menu from './components/layout/Menu.vue';
import MemberList from './components/member/MemberList.vue';
import DepartMenu from './components/member/DepartMenu.vue';
import UserList from './components/user/UserList.vue'
import GroupList from './components/group/GroupList.vue';
import AccountList from './components/system/AccountList.vue';
import AccountEdit from './components/system/AccountEdit.vue';
import Setting from './components/system/Setting.vue';
import LogList from './components/system/LogList.vue';


Vue.use(Router)

const limit_time=60*60*1000;   //session响应时间60分钟，超过清空


//加一个判断，如果没有登录就直接跳到登录页面,这边到时候用access_token或者session来判断
function checkLogin(to,from,next){
    //判断qy_code是否存在，不存在从storage里面取
    let state_qy_code=store.getters.getQyCode;
    if(to.path=='/depart/memberlist' && (state_qy_code=='' || state_qy_code==null)){
        let storage_qy_code=StorageAPI.get_storage('qy_code');
        store.commit('setQyCode',storage_qy_code);
    }
    //这边设置拦截，刷新页面判断state和缓存，判断过期时间
    var state_session_id=store.getters.getSessionId;
    //企业微信返回code的情况
    if(to.query.code!=undefined && to.query.state==state_session_id){
        StorageAPI.store_storage('qy_code',to.query.code);
        next('/depart/memberlist');
        return;
    }
    var over_limit=false;
    if(state_session_id==''){
        var storage_session_id=StorageAPI.get_storage('session_id');
        //缓存中也没有登录信息的时候就跳转登录
        if(storage_session_id=='' || storage_session_id=='undefined' || storage_session_id==null){
            next('/login');
            return;
        }else{
            //缓存中有信息，就让缓存中的数据给state更新一遍
            store.commit('setLogin',{
                code:1,
                info:{
                    username:StorageAPI.get_storage('username'),
                    session_id:StorageAPI.get_storage('session_id'),
                    user_id:StorageAPI.get_storage('user_id'),
                    role_id:StorageAPI.get_storage('role_id')
                }
            })
            over_limit=setLimitTime();
            //超时跳转回登录页，清空session
            if(!over_limit){
                store.commit('setLoginStatus',-1);
                next('/login');
            }else{
                next();
            }
        }
    }else{
        over_limit=setLimitTime();
        if(!over_limit){
            //将登录状态置为-1
            store.commit('setLoginStatus',-1);
            next('/login');
        }else{
            next();
        }
    }
}

//判断过期时间
function setLimitTime(){
    //是否有这个时间
    var start_time=StorageAPI.get_session_storage('start_limit_time');
    if(start_time!='undefined' && start_time!=null){
        //看到当前的时间是否超过超时时间
        var now_time=Date.now();
        if((parseInt(start_time)+limit_time)<now_time){
            //清空缓存，跳回登录页
            sessionStorage.clear();
            return false;
        }
        //不超过就重新计时
        StorageAPI.store_session_storage('start_limit_time',now_time);
        return true;
    }
    var now_time=Date.now();
    StorageAPI.store_session_storage('start_limit_time',now_time);
    return true;
}

//解决重复点击菜单报错的问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default  new Router({
    //嵌套路由，设置布局
    //mode: 'history',
    //base: '/',
    routes: [
        {
            path: '/',
            name:'layout',
            beforeEnter:checkLogin,
            component:Menu,
            children:[
                    {
                       path:'depart',
                       name:'depart',
                       beforeEnter:checkLogin,
                       component:DepartMenu,
                       children:[
                          {
                              path:'memberlist',
                              name:'memberlist',
                              component:MemberList
                          }
                       ]
                    },
                    {
                       path:'userlist',
                       name:'userlist',
                       beforeEnter:checkLogin,
                       component:UserList,
                    },
                    {
                       path:'grouplist',
                       name:'grouplist',
                       beforeEnter:checkLogin,
                       component:GroupList
                    },
                    {
                       path:'accountedit',
                       name:'accountedit',
                       beforeEnter:checkLogin,
                       component:AccountEdit
                    },
                    {
                       path:'accountlist',
                       name:'accountlist',
                       beforeEnter:checkLogin,
                       component:AccountList
                    },
                    {
                       path:'setting',
                       name:'setting',
                       beforeEnter:checkLogin,
                       component:Setting
                    },
                    {
                       path:'loglist',
                       name:'loglist',
                       beforeEnter:checkLogin,
                       component:LogList
                    }
                ]
        },
        {
            path:'/login',
            name:'login',
            component:Login
        }
    ]
})
